package com.ruoyi.share.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.share.mapper.ShareMapper;
import com.ruoyi.share.domain.Share;
import com.ruoyi.share.service.IShareService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

/**
 * 文件分享Service业务层处理
 *
 * @author huacai
 * @date 2024-07-10
 */
@Service
public class ShareServiceImpl implements IShareService
{
    @Autowired
    private ShareMapper shareMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询文件分享
     *
     * @param shareId 文件分享主键
     * @return 文件分享
     */
    @Override
    public Share selectShareByShareId(Integer shareId)
    {
        return shareMapper.selectShareByShareId(shareId);
    }

    /**
     * 查询文件分享列表
     *
     * @param share 文件分享
     * @return 文件分享
     */
    @Override
    public List<Share> selectShareList(Share share)
    {
        return shareMapper.selectShareList(share);
    }

    /**
     * 新增文件分享
     *
     * @param share 文件分享
     * @return 结果
     */
    @Override
    public int insertShare(Share share)
    {
        share.setCreateTime(DateUtils.getNowDate());
        return shareMapper.insertShare(share);
    }

    /**
     * 批量新增文件分享
     *
     * @param shares 文件分享List
     * @return 结果
     */
    @Override
    public int batchInsertShare(List<Share> shares)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(shares)) {
            try {
                for (int i = 0; i < shares.size(); i++) {
                    int row = shareMapper.insertShare(shares.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == shares.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改文件分享
     *
     * @param share 文件分享
     * @return 结果
     */
    @Override
    public int updateShare(Share share)
    {
        share.setUpdateTime(DateUtils.getNowDate());
        return shareMapper.updateShare(share);
    }

    /**
     * 批量删除文件分享
     *
     * @param shareIds 需要删除的文件分享主键
     * @return 结果
     */
    @Override
    public int deleteShareByShareIds(Integer[] shareIds)
    {
        return shareMapper.deleteShareByShareIds(shareIds);
    }

    /**
     * 删除文件分享信息
     *
     * @param shareId 文件分享主键
     * @return 结果
     */
    @Override
    public int deleteShareByShareId(Integer shareId)
    {
        return shareMapper.deleteShareByShareId(shareId);
    }
}
